package com.ll.beautymall.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ll.beautymall.R;
import com.ll.beautymall.bean.ModelContent;
import com.ll.beautymall.ui.home.categorydetail.view.CategoryDetailActivity;
import com.ll.beautymall.ui.home.zero.view.ZeroActivity;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/20.
 */
public class ModelAdapter extends BaseAdapter implements View.OnClickListener {
    private List<ModelContent> mData;
    private Context mContext;
    private Intent intent;
    private ModelContent.ContentDataBean content_data;
    private ModelContent.ContentDataBean content_dataTwo;

    public ModelAdapter(Context context) {
        this.mContext = context;
        intent = new Intent(mContext, CategoryDetailActivity.class);
    }

    public void setData(List<ModelContent> list) {
        this.mData = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return mData != null ? mData.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (getItemViewType(i) == 0 && mData.get(i).getContent_type().equals("0")) {
            view = LayoutInflater.from(mContext).inflate(R.layout.home_lv_header_img, viewGroup, false);
            ImageView img = (ImageView) view.findViewById(R.id.iv_zero);
            Picasso.with(mContext).load(mData.get(i).getContent_data().getPic_url()).into(img);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, ZeroActivity.class);
                    ModelContent modelContent = mData.get(i);
                    intent.putExtra("modelContent", modelContent);
                    mContext.startActivity(intent);
                }
            });
        } else if (getItemViewType(i) == 1) {
            view = LayoutInflater.from(mContext).inflate(R.layout.home_lv_header_model, viewGroup, false);
            ViewHolderOne holder = new ViewHolderOne(view);
            content_data = mData.get(i).getContent_data();
            holder.homeLvHeaderTvTitle.setText(mData.get(i).getContent_data().getTitle());
            holder.homeLvHeaderTvMsg.setText(mData.get(i).getContent_data().getSub_title());
            Picasso.with(mContext).load(content_data.getLeft_part().get(0).getPic_url())
                    .into(holder.homeLvHeaderIvLeft);
            Picasso.with(mContext).load(content_data.getRight_part().get(0).getPic_url())
                    .into(holder.homeLvHeaderIvRightTop);
            Picasso.with(mContext).load(content_data.getRight_part().get(1).getPic_url())
                    .into(holder.homeLvHeaderIvRightBottom);
            holder.homeLvHeaderIvLeft.setOnClickListener(this);
            holder.homeLvHeaderIvRightTop.setOnClickListener(this);
            holder.homeLvHeaderIvRightBottom.setOnClickListener(this);
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.home_lv_header_model_two, viewGroup, false);
            ViewHolder holder = new ViewHolder(view);
            content_dataTwo = mData.get(i).getContent_data();
            holder.homeLvHeaderTvTitle.setText(content_dataTwo.getTitle());
            holder.homeLvHeaderTvMsg.setText(content_dataTwo.getSub_title());
            Picasso.with(mContext).load(content_dataTwo.getLeft_part().get(0).getPic_url())
                    .into(holder.homeLvHeaderIvLeftTop);
            Picasso.with(mContext).load(content_dataTwo.getLeft_part().get(1).getPic_url())
                    .into(holder.homeLvHeaderIvLeftBottom);
            Picasso.with(mContext).load(content_dataTwo.getRight_part().get(0).getPic_url())
                    .into(holder.homeLvHeaderIvRightTop);
            Picasso.with(mContext).load(content_dataTwo.getRight_part().get(1).getPic_url())
                    .into(holder.homeLvHeaderIvRightBottom);
            holder.homeLvHeaderIvLeftTop.setOnClickListener(this);
            holder.homeLvHeaderIvLeftBottom.setOnClickListener(this);
            holder.homeLvHeaderIvRightTop.setOnClickListener(this);
            holder.homeLvHeaderIvRightBottom.setOnClickListener(this);
        }
        return view;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else if (position == 1) {
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.home_lv_header_iv_left:
                intent.putExtra("model", content_data.getLeft_part().get(0).getAction());
                mContext.startActivity(intent);
                break;
            case R.id.home_lv_header_iv_right_top:
                intent.putExtra("model", content_data.getRight_part().get(0).getAction());
                mContext.startActivity(intent);
                break;
            case R.id.home_lv_header_iv_right_bottom:
                intent.putExtra("model", content_data.getRight_part().get(1).getAction());
                mContext.startActivity(intent);
                break;

            case R.id.home_lv_header_iv_left_top:
                intent.putExtra("model", content_dataTwo.getLeft_part().get(0).getAction());
                mContext.startActivity(intent);
                break;
            case R.id.home_lv_header_iv_left_bottom:
                intent.putExtra("model", content_dataTwo.getLeft_part().get(1).getAction());
                mContext.startActivity(intent);
                break;
            case R.id.home_lv_header_iv_right_top2:
                intent.putExtra("model", content_dataTwo.getRight_part().get(0).getAction());
                mContext.startActivity(intent);
                break;
            case R.id.home_lv_header_iv_right_bottom2:
                intent.putExtra("model", content_dataTwo.getRight_part().get(1).getAction());
                mContext.startActivity(intent);
                break;

        }
    }

    static class ViewHolderOne {
        @BindView(R.id.home_lv_header_tv_title)
        TextView homeLvHeaderTvTitle;
        @BindView(R.id.home_lv_header_tv_msg)
        TextView homeLvHeaderTvMsg;
        @BindView(R.id.home_lv_header_iv_left)
        ImageView homeLvHeaderIvLeft;
        @BindView(R.id.home_lv_header_iv_right_top)
        ImageView homeLvHeaderIvRightTop;
        @BindView(R.id.home_lv_header_iv_right_bottom)
        ImageView homeLvHeaderIvRightBottom;

        ViewHolderOne(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder {
        @BindView(R.id.home_lv_header_tv_title)
        TextView homeLvHeaderTvTitle;
        @BindView(R.id.home_lv_header_tv_msg)
        TextView homeLvHeaderTvMsg;
        @BindView(R.id.home_lv_header_iv_left_top)
        ImageView homeLvHeaderIvLeftTop;
        @BindView(R.id.home_lv_header_iv_left_bottom)
        ImageView homeLvHeaderIvLeftBottom;
        @BindView(R.id.home_lv_header_iv_right_top2)
        ImageView homeLvHeaderIvRightTop;
        @BindView(R.id.home_lv_header_iv_right_bottom2)
        ImageView homeLvHeaderIvRightBottom;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
